package com.server.ejb;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


import com.server.entity.Item;
import com.server.entity.ItemComment;
import com.server.entity.User;
/**
 * Session Bean implementation class ItemBean
 */
@Stateless
@LocalBean
public class ItemBean implements ItemBeanLocal {

	
	@PersistenceContext
	private EntityManager em;
    /**
     * Default constructor. 
     */
    public ItemBean() {
        // TODO Auto-generated constructor stub
    }
    
    public Item getItem(String id){
    	return em.find(Item.class, id);
    }
    
    @SuppressWarnings("unchecked")
	public List<Item> getItemList(int pagesize,int start, int end){

    	ItemComment ic=new ItemComment();    	
    	ic.setComment("Hello!");
    	ic.setItemId("612fa82c-cebf-11e2-9801-e3f2148a79c0");
    	ic.setUserId("12345");
    	em.persist(ic);
    	
    	Query query= em.createNamedQuery("GetAllItems", Item.class);
    	List<Item> items=query.setMaxResults(pagesize).getResultList().subList(start, end);
    	return items;

    }
    
    public boolean setItem(Item item){
    	if(em.find(Item.class, item.getItemId())!=null){
    		em.merge(item);
    		
    	}
    	else em.persist(item);
    	return true;
    }

}
